package com.ruoyi.system.services;

import com.ruoyi.common.enums.PushTypeEnum;
import com.ruoyi.common.utils.SplitUtil;
import com.ruoyi.system.entity.PushFailedLoger;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 推送信息 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PushFailedLogerServices
{
    /**
     * 编辑推送信息
     * @return
     */
    public PushFailedLoger createushFailedLoger(String s) {

        PushFailedLoger pushFailedLoger = new PushFailedLoger();
        // 所有请求数据添加入新表  push_failed_loger
        HashMap<String,String> resultMap = SplitUtil.getValue(s);
        if(resultMap.isEmpty()){
           return pushFailedLoger;
        }

        // 所有请求数据添加入新表  push_failed_loger
        // 系统时间
        long timestamp = System.currentTimeMillis();
        Date date = new Date(timestamp);

        PushTypeEnum pushTypeEnum = PushTypeEnum.getPushTypeEnum(resultMap.get("url"));

        if(pushTypeEnum.toString().contains("DOUBLE_PREVENTION")){
            // 模块
            pushFailedLoger.setPushModule("双重预防");
        } else if (pushTypeEnum.toString().contains("SPECIAL_INSPECTION")) {
            // 模块
            pushFailedLoger.setPushModule("专项检查");
        } else if (pushTypeEnum.toString().contains("PLATFORM_ALARM")) {
            // 模块
            pushFailedLoger.setPushModule("平台报警");
        }

        String pushType = PushTypeEnum.getPushType(resultMap.get("url"));
        // 事件类型
        pushFailedLoger.setPushType(pushType);

        // 推送时间
        pushFailedLoger.setPushCreateTime(date);
        if("200".equals(resultMap.get("code"))){
            // 返回批次号
            pushFailedLoger.setBatchId(resultMap.get("batchId").isEmpty()? null : resultMap.get("batchId"));
            // 推送条数
            pushFailedLoger.setPushNum(resultMap.get("totalCount").isEmpty()? null : Integer.parseInt(resultMap.get("totalCount")));
            // 推送失败条数
            pushFailedLoger.setPushFailureNum(resultMap.get("errorCount").isEmpty()? null : Integer.parseInt(resultMap.get("errorCount")));
            // 推送结果
            pushFailedLoger.setPushResult(1);
        }else{
            // 推送结果
            pushFailedLoger.setPushResult(0);
        }
        // 备注（推送信息）
        pushFailedLoger.setPushInfo(s);

        return pushFailedLoger;
    }

}
